<?php
namespace app\api\controller;
use think\facade\Db;
class LandmarkTrademark extends common
{
    private $dbname = 'landmark_trademark';


    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {

        $data = input('get.');

        $where = [];
        if(!empty($data['name'])){
            $where[] = ['landmark_trademark.name
                |landmark_trademark.content
                ','like','%'.$data['name'].'%'];
        }

        if(!empty($data['status'])){
            $where[] = ['landmark_trademark.status','=',$data['status']];
        }else{
            $where[] = ['landmark_trademark.status','in',[0,1,2]];
        }
        if(!empty($data['product_id'])){
            $where[] = ['landmark_trademark.product_id','=',$data['product_id']];
        }

        $list = Db::view($this->dbname,'*')
            ->where($where)
            ->view('product', 'name as product_name', 'product.id = landmark_trademark.product_id', 'LEFT')
            ->view('company', 'name as company_name', 'company.id = product.company_id', 'LEFT')

            ->order('id asc')
            ->paginate([
                'list_rows'=> $data['limit'],
                'var_page' => 'page',
            ])->toArray();

        foreach ($list['data'] as $k=>$v){
            $img = $v['image']?json_decode($v['image']):[];
            $list['data'][$k]['image'] = $img;
        }
        return re_success('获取成功',$list);
//        return re_list($list);
    }
    /**
     * 列表分页获取
     * @return string
     */
    public function getSeleList()
    {
        $data = input('get.');
        $where = [];
        if(!empty($data['id']) ){
            $list = Db::name($this->dbname)->whereOr('id','=',$data['id'])->whereOr('name','like','%'.$data['name'].'%')->field('id,rank,status')->select()->toArray();

        }else if(!empty($data['product_id']) ){
            $list = Db::name($this->dbname)
                ->where('status',1)
                ->where('product_id',$data['product_id'])->field('id,rank,status')->select()->toArray();

        }else{
            $list = Db::name($this->dbname)->field('id,rank,status')->select()->toArray();

        }
        return re_success('获取成功',$list);
    }
    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {

        $data = input('post.');
        $data['image'] = $data['image']?json_encode($data['image']):null;
        $data['user_id'] = $this->getThisUser()['uid'];
        //模型数据验证
        $model = new \app\api\model\LandmarkTrademark();

        $er = $model->add_vali($data);
        if($er){
            return  $er;
        }
        //模型写入
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        //模型验证
        $data['image'] = $data['image']?json_encode($data['image']):null;
        $data['user_id'] = $this->getThisUser()['uid'];
        $model = new \app\api\model\LandmarkTrademark();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        return $model->edit($data);

    }

    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $info = Db::name($this->dbname)
            ->find(input('get.id'));
        if($info){
            $info['image'] = $info['image']?json_decode($info['image']):[];
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }


    /**
     * 删除
     * @return false|string|void
     */
    public function dele()
    {
        $data = input('delete.');
        $model = new \app\api\model\LandmarkTrademark();
        $er = $model->dele_vali($data);
        if($er){
            return  $er;
        }

        return $model->dele($data);
    }


    /**
     * 修改
     * @return false|string|void
     */
    public function updateStatus()
    {
        $data = input('put.');
        //模型验证
        $model = new \app\api\model\LandmarkTrademark();
        $er = $model->edit_status_vali($data);
        if($er){
            return  $er;
        }
        return $model->editStatus($data);

    }
}
